package com.um.psystem.controller.faultManage;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.um.psystem.controller.BaseController;
import com.um.psystem.enums.ResponseEnum;
import com.um.psystem.model.vo.DataGrid;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.faultService.IFaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2021/12/22 8:38
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "/fault")
public class FaultManageController extends BaseController {

    @Autowired
    IFaultService faultService;

    @RequestMapping(value = "/getFaultList")
    @ResponseBody
    public DataGrid getFaultList(@RequestParam Map map) {
        if (map.containsKey("fault_type") && map.get("fault_type").equals(""))
        { map.put("fault_type",null); }
        if (map.containsKey("workshop") && map.get("workshop").equals(""))
        { map.put("workshop",null); }
        if (map.containsKey("line_id") && map.get("line_id").equals(""))
        { map.put("line_id",null); }

        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));
        map.put("startIndex", (page - 1) * rows);
        map.put("rows", rows);

        List<Map<String, Object>> FaultList = faultService.getItemList(map);
        int count = 0;
        if (FaultList != null && FaultList.size() > 0)
            count = faultService.getItemListCount(map);
        return buildDataGrid(FaultList, count);
    }

    @RequestMapping(value = "/getLineList")
    @ResponseBody
    public DataGrid getLineList(@RequestParam Map map) {
        List<Map<String, Object>> LineList = faultService.getLineList(map);
        int count = 0;
        if (LineList != null && LineList.size() > 0)
            count = LineList.size();
        return buildDataGrid(LineList, count);
    }

    @RequestMapping(value = "/getDeviceList")
    @ResponseBody
    public DataGrid getDeviceList(@RequestParam Map map) {
        List<Map<String, Object>> DeviceList = faultService.getDeviceList(map);
        int count = 0;
        if (DeviceList != null && DeviceList.size() > 0)
            count = DeviceList.size();
        return buildDataGrid(DeviceList, count);
    }

    @RequestMapping(value = "/getUnitList")
    @ResponseBody
    public DataGrid getUnitList(@RequestParam Map map) {
        List<Map<String, Object>> UnitList = faultService.getUnitList(map);
        int count = 0;
        if (UnitList != null && UnitList.size() > 0)
            count = UnitList.size();
        return buildDataGrid(UnitList, count);
    }

    //----------Fault 接口----------

    @RequestMapping(value = "/deleFault", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Integer> deleFault(String fault_id) {
        Integer id = Integer.parseInt(fault_id);
        Integer rowcount = faultService.removeItem(id);
        return  JsonResult.success(rowcount);
    }

    @RequestMapping(value = "/addFault", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addFault(@RequestParam Map map) {
        map.put("fault_id",null);
        String jsonstr = map.get("unit_list").toString();
        List<Map<String,Object>> list =(new Gson()).fromJson(jsonstr,new TypeToken<List<Map<String,Object>>>(){}.getType());
        faultService.addItem(map);
        for (Map map1 : list)
        {
            map1.put("fid",map.get("fault_id"));
        }
        if(list.size()>0)
        {faultService.addUnitFromBat(list);}
        return JsonResult.success();
    }

    @RequestMapping(value = "/editFault", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editFault(@RequestParam Map map) {
        return JsonResult.success(faultService.changeItem(map));
    }

    //-----------LineType 接口-----------

    @RequestMapping(value = "/deleLine", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Integer> deleLine(String line_id) {
        Integer id = Integer.parseInt(line_id);
        Integer rows = faultService.removeLine(id);
        if (rows!=null)
        {return JsonResult.success();}
        else
        {return JsonResult.error(408,"此线体下有设备记录,请先全部删除此线体下的设备！");}

    }

    @RequestMapping(value = "/addLine", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Integer> addLine(@RequestParam Map map) {
        return JsonResult.success(faultService.addLine(map));
    }

    @RequestMapping(value = "/editLine", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editLine(@RequestParam Map map) {
        return JsonResult.success(faultService.changeLine(map));
    }

    //-----------Device 接口-----------

    @RequestMapping(value = "/deleDevice", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Integer> deleDevice(String device_id) {
        Integer id = Integer.parseInt(device_id);
        Integer rows = faultService.removeDevice(id);
        if (rows!=null)
        {return JsonResult.success();}
        else
        {return JsonResult.error(408,"系统有故障记录是基于此设备的，请删除此设备的所有故障记录后重试！");}

    }

    @RequestMapping(value = "/addDevice", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Integer> addDevice(@RequestParam Map map) {
        return JsonResult.success(faultService.addDevice(map));
    }

    @RequestMapping(value = "/editDevice", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editDevice(@RequestParam Map map) {
        return JsonResult.success(faultService.changeDevice(map));
    }

    //-----------Unit 接口-----------

    @RequestMapping(value = "/deleUnit", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Integer> deleUnit(String fault_unit_id) {
        Integer id = Integer.parseInt(fault_unit_id);
        return JsonResult.success(faultService.removeUnit(id));
    }

    @RequestMapping(value = "/addUnit", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Integer> addUnit(@RequestParam Map map) {
        Integer fid=null,unitcount=null;
        Integer type_dtl_id=null;
        if (map.get("fault_id")!=null)
        {fid=Integer.parseInt(map.get("fault_id").toString());}
        if (map.get("type_dtl_id")!=null)
        {type_dtl_id=Integer.parseInt(map.get("type_dtl_id").toString());}
        if (map.get("unit_count")!=null)
        {unitcount=Integer.parseInt(map.get("unit_count").toString());}

        return JsonResult.success(faultService.addUnit(fid,type_dtl_id,unitcount));
    }

    @RequestMapping(value = "/editUnit", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editUnit(@RequestParam Map map) {
        Integer fuid=null,unitcount=null;
        Integer typeDtlId=null;
        if (map.get("fault_unit_id")!=null)
        {fuid=Integer.parseInt(map.get("fault_unit_id").toString());}
        if (map.get("type_dtl_id")!=null)
        {typeDtlId=Integer.parseInt(map.get("type_dtl_id").toString());}
        if (map.get("unit_count")!=null)
        {unitcount=Integer.parseInt(map.get("unit_count").toString());}
        return JsonResult.success(faultService.changeUnit(fuid,typeDtlId,unitcount));
    }

    @RequestMapping(value = "/exportExceptionFaultData",method = RequestMethod.GET)
    public void exportExceptionFaultData(HttpServletResponse response, @RequestParam Map map)
    {
        System.out.println(map);
        for (Object key : map.keySet())
        {
            String str = map.get(key).toString();
            try {
                map.put(key,new String(str.getBytes("ISO-8859-1"),"UTF-8"));
            }catch (UnsupportedEncodingException e)
            {e.printStackTrace();}
        }
        if (map.get("fault_type")!=null && map.get("fault_type").equals(""))
        {map.put("fault_type",null);}
        if (map.get("line_id")!=null && map.get("line_id").equals(""))
        {map.put("line_id",null);}
        faultService.exportExceptionFaultData(response,map);
    }

    @RequestMapping(value = "/moveDevice", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult moveDevice(@RequestParam Map map) {
        if (map.get("line_id")!=null && map.get("device_id")!=null)
        {
            if(faultService.changeDevice(map)>0)
            { return JsonResult.success();}
            else
            {return JsonResult.error(409,"修改失败");}
        }
        else
        {return JsonResult.error(400,"参数错误");}
    }


    @RequestMapping(value = "/getLineTypePieData", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<Map<String,Object>>> getLineTypePieData(@RequestParam Map map) {
        List<Map<String,Object>> list = faultService.getLineTypePieData(map);
        if (list!=null)
        {return JsonResult.success(list);}
        else
        {return JsonResult.error(404,"获取数据失败");}

    }

    @RequestMapping(value = "/getWorkShopPieData", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<Map<String,Object>>> getWorkShopPieData(@RequestParam Map map) {
        List<Map<String,Object>> list = faultService.getWorkShopPieData(map);
        if (list!=null)
        {return JsonResult.success(list);}
        else
        {return JsonResult.error(404,"获取数据失败");}

    }

    @RequestMapping(value = "/getUnitsPieData", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<Map<String,Object>>> getUnitsPieData(@RequestParam Map map) {
        List<Map<String,Object>> list = faultService.getUnitsPieData(map);
        if (list!=null)
        {return JsonResult.success(list);}
        else
        {return JsonResult.error(404,"获取数据失败");}

    }

    @RequestMapping(value = "/getFaultTypePieData", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<Map<String,Object>>> getFaultTypePieData(@RequestParam Map map) {
        List<Map<String,Object>> list = faultService.getFaultTypePieData(map);
        if (list!=null)
        {return JsonResult.success(list);}
        else
        {return JsonResult.error(404,"获取数据失败");}

    }

    @RequestMapping(value = "/getTypeDtlList")
    @ResponseBody
    public JsonResult<List<Map<String,Object>>> getTypeDtlList(@RequestParam Map map) {
        List<Map<String,Object>> list = faultService.getTypeDtlList(map);
        if (list!=null)
        {return JsonResult.success(list);}
        else
        {return JsonResult.error(404,"获取数据失败");}

    }
}
